package edu.columbia.gate.plugins;


import gate.Annotation;
import gate.AnnotationSet;
import gate.Document;
import gate.FeatureMap;
import gate.SimpleAnnotation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import java.util.*;

import gate.*;
import gate.corpora.DocumentContentImpl;
import gate.creole.*;
import gate.creole.gazetteer.AbstractGazetteer;
import gate.util.GateRuntimeException;
import gate.util.InvalidOffsetException;

public class DocumentFilter extends AbstractLanguageAnalyser implements
		ProcessingResource
{

	public static final String TRANSD_DOCUMENT_PARAMETER_NAME = "document";

	public static final String ANNOTATION_SET_NAME_PARAMETER_NAME = "annotationSetName";

	public static final String ANNOTATION_TYPE_PARAMETER_NAME = "annotationType";

	protected String annotationSetName;
	protected String annotationType;

	/** Initialise this resource, and return it. */
	public Resource init() throws ResourceInstantiationException
	{
		return super.init();
	} // init()

	public void reInit() throws ResourceInstantiationException
	{
		init();
	} // reInit()

	/** Run the resource. */
	public void execute() throws ExecutionException
	{

		if (document == null)
			throw new GateRuntimeException("No document to process!");

		AnnotationSet annotSet = null;
		if (annotationSetName == null || annotationSetName.isEmpty())
		{
			annotSet = document.getAnnotations();
		} 
		else
		{
			annotSet = document.getAnnotations(annotationSetName);
		}
		if (annotSet == null)
			throw new GateRuntimeException("Annotation set '"
					+ annotationSetName + "' was not found in the document.");

		String newContent = this.document.getContent().toString();

		Annotation[] annots = new Annotation[annotSet.size()];
		annotSet.toArray(annots);
		ArrayList<Annotation> annotsSorted = new ArrayList<Annotation>();
		for (int i = 0; i < annots.length; i++)
		{
			annotsSorted.add(annots[i]);
		}
		Collections.sort(annotsSorted, new AnnotationComparator());

		int offset = 0;
		long textStart, textEnd, lastTextStart = -1;
		ArrayList<ReplacedDocumentContent> replacedTexts = new ArrayList<ReplacedDocumentContent>(annotsSorted.size());
		
		for (int i = 0; i < annotsSorted.size(); i++)
		{
			SimpleAnnotation annot = (SimpleAnnotation) annotsSorted.get(i);
			if(annot.getType().equals(annotationType))
			{
				textStart = annot.getStartNode().getOffset().intValue() + offset;
				textEnd = annot.getEndNode().getOffset().intValue() + offset;
				
				newContent = newContent.substring((int) 0, (int) textStart) + newContent.substring((int) textEnd);
				offset += textEnd - textStart;
			}
		}

		DocumentUtils.deleteAllAnnotations(document);
		
		DocumentUtils.replaceDocumentContent(document, newContent);
				
	} // execute()

	public String getAnnotationSetName()
	{
		return this.annotationSetName;
	}

	public void setAnnotationSetName(String annotationSetName)
	{
		this.annotationSetName = annotationSetName;
	}

	public String getAnnotationType()
	{
		return this.annotationType;
	}

	public void setAnnotationType(String annotationType)
	{
		this.annotationType = annotationType;
	}


} // class AnnotationSetTransfer
